package housekeeping.service.Impl;

import housekeeping.mapper.UserAccountsMapper;
import housekeeping.pojo.UserAccounts;
import housekeeping.service.UserAccountsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @version 1.0
 * @Author Du
 * @Date 2025/7/7 14:29
 * @注释
 */
@Service
public class UserAccountServiceImpl implements UserAccountsService {
    @Autowired
    private UserAccountsMapper userAccountsMapper;
    @Override
    public List<UserAccounts> getUserAccounts() {
        List<UserAccounts> list = userAccountsMapper.selectList(null);
        return list;
    }
    @Override
    public void deleteUserAccounts(Integer id) {
        int result = userAccountsMapper.deleteById(id);
        if(result < 1){
            throw new RuntimeException("删除账户失败");
        }
    }

    @Override
    public UserAccounts selectById(Integer id) {
        UserAccounts userAccounts = userAccountsMapper.selectById(id);
        if(userAccounts != null){
            return userAccounts;
        }
        else{
            throw new RuntimeException("查询失败");
        }
    }

    @Override
    public void updateUserAccounts(UserAccounts userAccounts) {
        UserAccounts oldUserAccounts = userAccountsMapper.selectById(userAccounts.getId());
        if(oldUserAccounts != null){
            int result = userAccountsMapper.updateById(userAccounts);
            if(result < 1){
                throw new RuntimeException("更新失败");
            }
        }
        else{
            throw new RuntimeException("更新失败");
        }
    }
}
